import sqlite3

# 连接到数据库
conn = sqlite3.connect('./openmemory.db')
cursor = conn.cursor()

# 查询所有应用及其记忆数量
cursor.execute("SELECT id, name, created_at FROM apps ORDER BY created_at")
apps = cursor.fetchall()

print('所有应用:')
for app in apps:
    cursor.execute("SELECT COUNT(*) FROM memories WHERE app_id = ?", (app[0],))
    count = cursor.fetchone()[0]
    print(f'ID: {app[0]}, Name: {app[1]}, Created: {app[2]}, Memories: {count}')

# 查询所有记忆及其对应的应用
cursor.execute("SELECT COUNT(*) FROM memories")
total_memories = cursor.fetchone()[0]
print(f'\n总记忆数量: {total_memories}')

conn.close()